package org.lansir.af.ui;

import org.lansir.af.database.DataTable;
import org.lansir.af.database.Database;
import org.lansir.af.gtalk.GtalkApplication;
import org.lansir.af.util.Prefs;
import org.lansir.af.util.Utils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;

public class CommonFragment extends Fragment {
	private static final String TAG = "CommonFragment";
	
	public static final String requestBundleRowId = "RowId";
	public static final String requestBundleTableUpdated = "TableUpdated";
	public static final String requestBundleActivityTitle = "ActvitiyTitle";
	
	public static final int REQUEST_CODE_DEFAULT = 0;
	
	public enum StartMode {
		Default, EDIT, NEW, VIEW
	};
	
	public enum Action {
		Default, ShowMsg
	};
	
	public class ViewType{
		public static final int NONE = 0;
		public static final int DAY = 1;
		public static final int WEEK = 2;
		public static final int MONTH = 3;
		public static final int TODAY_ALARM = 4;
		public static final int GROUP = 5;
	}
	
	public enum SnoozeAction {
		Snooze, Clear;
		public static int convertToInt(SnoozeAction aAction){
			switch(aAction){
			case Snooze:
				return 0;
			case Clear:
				return 1;
			}
			return 1;	
		}
		
		public static SnoozeAction convertFromInt(int iAction){
			switch(iAction){
			case 0:
				return Snooze;
			case 1:
				return Clear;
			}
			return Clear;	
		}
	}
	
	protected Utils utils;
	protected Database userdb;

	private StartMode startMode = StartMode.Default;
	private Bundle bundleDataStartup = new Bundle();
	protected Bundle bundleOtherDataStartup = new Bundle();
	protected Bundle bundleOtherDataClose = new Bundle();
	protected GtalkApplication mGtalkApplication;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.utils = new Utils(getActivity());
		this.userdb = new Database(getActivity());
		this.mGtalkApplication = (GtalkApplication) getActivity().getApplication();
	}
	


	
	@Override
	public void onDestroy() {
		this.userdb.close();
		super.onDestroy();
	}

	public void openActivityForResult(int iRequestCode,String sAction){
		Intent intent = new Intent(sAction);
		bundleDataStartup.clear();
		bundleDataStartup.putAll(bundleOtherDataStartup);
		intent.putExtras(bundleDataStartup);
		
		startActivityForResult(intent, iRequestCode);
	}
	
	public void openActivity(String sAction){
		Intent intent = new Intent(sAction);
		bundleDataStartup.clear();
		bundleDataStartup.putAll(bundleOtherDataStartup);
		intent.putExtras(bundleDataStartup);
		
		startActivity(intent);
	}
	
	public StartMode getStartMode() {
		startMode = StartMode.Default;
		String sAction = getActivity().getIntent().getAction();
		if (sAction.contains("ACTION_MODE_NEW")){
			startMode = StartMode.NEW;
		}else if(sAction.contains("ACTION_MODE_EDIT")){
			startMode = StartMode.EDIT;
		}else if(sAction.contains("ACTION_MODE_VIEW")){
			startMode = StartMode.VIEW;
		}
		return startMode;
	}
	
	public boolean openDataForEdit(DataTable data) {
		long lRowId = requestedRowId();
		Database.Result result = data.getRowDataForEdit(lRowId);
		if (result == Database.Result.Success) {
			// save date values for change test
			return true;
		} else {
			// return fail result for caller
			int iMsgId = Database.getErrDesc(result);
			Bundle extras = putBundledMessage(iMsgId);
			setIntentResult("", Activity.RESULT_CANCELED, extras);

			return false;
		}
	}
	
	private Bundle putBundledMessage(int iMsgId) {
		Bundle extras = new Bundle();
		extras.putInt("msgResStrId", iMsgId);
		extras.putInt("msgType", Utils.MSGTYPE_ERROR);
		extras.putString("action", Action.ShowMsg.toString());
		return extras;
	}
	
	protected void setIntentResult(String action, int resultCode, Bundle bundle) {
		Intent intentData = new Intent(action);
		intentData.putExtras(bundle);
		getActivity().setResult(resultCode, intentData);
	}
	
	public void closeActivity(DataTable data) {
		bundleOtherDataClose.putString(requestBundleTableUpdated, data.getTableName());

		setIntentResult("", Activity.RESULT_OK, bundleOtherDataClose);
		getActivity().finish();
	}
	
	/**
	 * 如果为新的APPT，RowId=-1
	 * @return
	 */
	public long requestedRowId() {
		return getActivity().getIntent().getExtras().getLong(requestBundleRowId);
	}
	
	protected void setResult(int iResult){
		getActivity().setResult(iResult);
	}
	
	protected void finish(){
		getActivity().finish();
	}

}
